package com.yiren.dbaa.module.practice_set.map.view.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.map.contract.MapHistoryContract;
import com.yiren.dbaa.module.practice_set.map.presenter.MapHistoryPresenter;
import com.yiren.dbaa.module.practice_set.map.view.adapter.MapVersionAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/11/29 16:38
 * @description: 地图历史版本
 */
public class MapHistoryActivity extends BaseActivity implements MapHistoryContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lv_version_map)
    ListView lvVersionMap;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.tv_tips)
    TextView tvTips;

    private MapEntity map;
    private MapHistoryContract.Presenter mapHistoryPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_map);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        tvTitle.setText("地图回退版本");
        map = (MapEntity) this.getIntent().getSerializableExtra("map");
        mapHistoryPresenter = new MapHistoryPresenter(this);
        UserParamConfigBean userParamConfigBean = LoadConfigApi.loadUserParamConfig(this);
        if (map != null) {
            mapHistoryPresenter.loadVersionList(this, userParamConfigBean.getUserInfo().getId().toString(), map);
        }
    }

    @OnClick(R.id.iv_back)
    public void onClick() {
        finish();
    }

    @Override
    public void showVersionList(List<MapEntity> mapList) {
        if (mapList.size() == 0) {
            llNoData.setVisibility(View.VISIBLE);
            lvVersionMap.setVisibility(View.GONE);
            tvTips.setText("当前暂无历史版本数据!");
        } else {
            llNoData.setVisibility(View.GONE);
            lvVersionMap.setVisibility(View.VISIBLE);
            MapVersionAdapter mapVersionAdapter = new MapVersionAdapter(this, mapList);
            lvVersionMap.setAdapter(mapVersionAdapter);
            mapVersionAdapter.setOnManageClickListener(position -> {
                CommonDialog.showTwoBtn(this, "操作确认", "下载会覆盖本地考规，确定下载吗？", "取消", "下载", (dialog, which) -> {
                    if (which == -2) {
                        dialog.dismiss();
                    } else {
                        mapList.get(position).setSiteCode(map.getSiteCode());
                        mapList.get(position).setName(map.getName());
                        mapList.get(position).setMapVerLast(map.getMapVerLast());
                        mapList.get(position).setAddress(map.getAddress());
                        mapHistoryPresenter.downLoadMapData(mapList.get(position), this);
                        dialog.dismiss();
                    }
                });
            });
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}